<?php

namespace API\App\Model;

use Hx\App;

class Blog {

    /**
     * 数据库对象
     * @var \Hx\Db
     */
    private $db;

    /**
     * 构造函数
     */
    public function __construct() {
        $this->db = App::Db();
    }

    public function getList($condition = null, $page, $size) {
        $col = 'N.id AS id,title,nodeKey,N.createDate AS createDate,N.desc AS desc,categoryId,userId,commentCount,N.status AS status';
        $col .= ',C.name AS categoryName,C.keyString AS categoryKey';
        $col .= ',U.nickName AS userName';
        $sql = App::Sql('HxSite_Node AS N', $col)
            ->leftJoin('HxSite_Category AS C', 'N.categoryId = C.id')
            ->leftJoin('HxSite_User AS U', 'U.id = N.userId')
            ->where('nodeType = "blog" AND ' . $condition)
            ->order('createDate DESC')
            ->page($page, $size)
            ->select();
        $data['blog'] = $this->db->queryAll($sql);
        $sql = App::Sql('HxSite_Node AS N')
            ->where('nodeType = "blog" AND ' . $condition)
            ->order('createDate DESC')
            ->count();
        $all = $this->db->query($sql)->num;
        $data['count'] = array(
            'all' => $all,
            'pages' => (int)($all / $size + 0.999),
            'current' => $page,
            'begin' => ($page - 1) * $size >= $all ? $all : ($page - 1) * $size + 1,
            'end' => $page * $size >= $all ? $all : $page * $size
        );
        return $data;
    }

    public function getOne($id) {
        $col = 'N.id AS id,title,nodeKey,N.createDate AS createDate,N.content AS content,categoryId,userId,commentCount,hitCount';
        $col .= ',C.name AS categoryName,C.keyString AS categoryKey';
        $col .= ',U.nickName AS userName';
        $sql = App::Sql('HxSite_Node AS N', $col)
            ->leftJoin('HxSite_Category AS C', 'N.categoryId = C.id')
            ->leftJoin('HxSite_User AS U', 'U.id = N.userId')
            ->where('N.id = :id AND nodeType = "blog"')
            ->select();
        return $this->db->query($sql, array('id' => $id));
    }

    public function getArchive() {
        $sql = App::Sql('HxSite_Node', 'title,nodeKey,createDate,commentCount')
            ->order('createDate DESC')
            ->select();
        $tmpData = $this->db->queryAll($sql);
        $returnData = array();
        foreach ($tmpData as $blog) {
            $month = 'month-' . date('Y-m', $blog->createDate);
            $returnData[$month][] = $blog;
        }
        return $returnData;
    }

    public function addBlog($data) {
        if ($this->isKeyExist($data['nodeKey'])) {
            return false;
        }
        $data['desc'] = substr($data['content'], 0, stripos($data['content'], '<hr class="blog-break" />'));
        if (!$data['status']) {
            $data['status'] = 'public';
        }
        $data['nodeType'] = 'blog';
        $data['createDate'] = time();
        $data['editDate'] = time();
        $col = 'title,nodeKey,desc,content,createDate,editDate,userId,categoryId,';
        $col .= 'nodeType,status,isComment,isFeed';
        $sql = App::Sql('HxSite_Node', $col)
            ->insert();
        if (!$this->db->exec($sql, $data)) {
            return false;
        }
        return $this->getOne($this->db->lastId());
    }

    private function isKeyExist($key, $id = 0) {
        $sql = App::Sql('HxSite_Node', 'id')
            ->where('nodeKey = :key')
            ->select();
        $data = $this->db->query($sql, array('key' => $key));
        if (!$data) {
            return false;
        }
        if ($id) {
            if ($data->id == $id) {
                return false;
            }
        }
        return true;
    }

    public function changeStatus($id, $status) {
        $sql = App::Sql('HxSite_Node', 'status')
            ->where('id = :id')
            ->update();
        return $this->db->exec($sql, array(
            'id' => $id,
            'status' => $status
        ));
    }
}